package com.example.user.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.mapper.GdUserMapper;
import com.example.pojo.GdUser;
import com.example.service.RpcRegisterService;
import com.example.service.RpcThirdPartyService;
import com.example.utils.MD5;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by LENOVO on 2018/9/22.
 */
@Component
@Service(interfaceClass= RpcRegisterService.class)
public class RpcRegisterServiceImpl implements RpcRegisterService {

    @Resource
    private GdUserMapper gdUserMapper;
    @Resource
    private RpcThirdPartyService thirdPartyService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 手机注册
     * @param user
     */
    @Override
    public void registerUserByPhone(GdUser user) throws Exception{
        user.setCreationdate(new Date());
        gdUserMapper.insertSelective(user);
        String VerificationCode= MD5.createRandom();
        thirdPartyService.sendSms(user.getUsercode(),VerificationCode);
        stringRedisTemplate.opsForValue().set(user.getUsercode()+"的验证码", VerificationCode,60, TimeUnit.SECONDS);
    }

    /**
     * 短信验证
     * @param phone
     * @param code
     * @return
     */
    @Override
    public boolean validateSms(String phone,String code) throws Exception{
        boolean result=false;
        if(stringRedisTemplate.hasKey(phone+"的验证码"))
        {
            String verificationCode=stringRedisTemplate.opsForValue().get(phone+"的验证码");

            if(verificationCode.equals(code))
            {
                GdUser user=gdUserMapper.findByUserCode(phone);
                if(null!=user)
                {
                    user.setCreationdate(new Date());
                    gdUserMapper.updateByPrimaryKeySelective(user);
                    result=true;
                }
                else
                {
                    result=false;
                }
            }
            else
            {
                result=false;
            }
        }
        else
        {
            result=false;
        }
        return result;
    }

    /**
     * 邮箱注册
     * @param user
     * @throws Exception
     */
    @Override
    public void registerUserByMail(GdUser user) throws Exception {
        user.setActivated(1L);
        gdUserMapper.insertSelective(user);
        String activationCode = MD5.getMd5(new Date().toLocaleString(), 32);
        thirdPartyService.sendMail(user.getUsercode(),activationCode);
        stringRedisTemplate.opsForValue().set(user.getUsercode()+"的激活码", activationCode);
    }

    /**
     * 邮箱激活
     * @param mail
     * @param code
     * @return
     */
    @Override
    public boolean activateMail(String mail,String code) throws Exception{
        boolean result=false;
        if(stringRedisTemplate.hasKey(mail+"的激活码"))
        {
            String activationCode=stringRedisTemplate.opsForValue().get(mail+"的激活码");

            if(activationCode.equals(code))
            {
                GdUser user=gdUserMapper.findByUserCode(mail);
                if(null!=user)
                {
                    user.setActivated(2L);
                    user.setCreationdate(new Date());
                    gdUserMapper.updateByPrimaryKeySelective(user);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /**
     * 查看用户
     * @param userCode
     * @return
     */
    @Override
    public boolean findUserByUserCode(String userCode)throws Exception
    {
        boolean result=false;
        GdUser user=gdUserMapper.findByUserCode(userCode);
        if(null!=user) {
            result = true;
        }
        return  result;
    }
}
